package com.wiscamp.ninechapters.configuration;

import cube.ddd.utils.ResourceUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class CustomAccessDeniedHandler implements AuthenticationEntryPoint {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void commence(jakarta.servlet.http.HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException)
            throws IOException,
            jakarta.servlet.ServletException {
        logger.error("认证失败 URL: " + request.getRequestURL());
        logger.error("认证失败：", authException.getMessage());
        ResourceUtils.getExceptionResponse(request, response, authException);
    }

}
